//
//  AVOSManger.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/9/11.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//
#import "AVOSManger.h"
#import "ASIFormDataRequest.h"
#import "TLLoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "NSDictionary+AVOS.h"
#import "NSDictionary+JSON.h"

#import "NSNumber+Increment.h"

#import "TXYManger.h"
#import "TXYRespCode.h"
#import "TXYVideoThumbFetcher.h"
#import "NSArray+TXYVideoInfoExtract.h"

/*
 NSArray *v_urls,NSArray *v_names,NSArray *v_ctimes,NSArray *v_descs, NSArray *likes,NSArray *indexs,NSUInteger videoCount
 */
@interface AVOSManger ()

@property (nonatomic,readonly) NSDictionary *staticList;

@property (nonatomic,strong) NSMutableArray *v_urls;
@property (nonatomic,strong) NSMutableArray *v_names;
@property (nonatomic,strong) NSMutableArray *pub_user;
@property (nonatomic,strong) NSMutableArray *v_ctimes;
@property (nonatomic,strong) NSMutableArray *v_descs;
@property (nonatomic,strong) NSMutableArray *likes;
@property (nonatomic,strong) NSMutableArray *indexs;
@property (nonatomic,strong) NSMutableArray *v_ids;
@property (nonatomic,strong) NSMutableArray *v_cate;
@property (nonatomic,strong) NSMutableArray *v_cmt_count;
@end

@implementation AVOSManger
@synthesize staticList,v_urls,v_ctimes,v_descs,v_names,pub_user,likes,indexs,v_ids,v_cate,v_cmt_count;

- (id)init{
    self = [super init];
    
    if(self)
    {
        v_urls = [[NSMutableArray alloc] init];
        v_names = [[NSMutableArray alloc] init];
        pub_user = [[NSMutableArray alloc] init];
        v_ctimes = [[NSMutableArray alloc] init];
        v_descs = [[NSMutableArray alloc] init];
        likes = [[NSMutableArray alloc] init];
        indexs = [[NSMutableArray alloc] init];
        v_ids = [[NSMutableArray alloc] init];
        v_cate = [[NSMutableArray alloc] init];
        v_cmt_count = [[NSMutableArray alloc] init];
    }
    
    return self;
}
+ (AVOSManger *)sharedManger{
    static dispatch_once_t onceToken;
    static AVOSManger *manger;
    dispatch_once(&onceToken, ^{
        manger = [[AVOSManger alloc] init];
    });
    
    return manger;
}

- (void)sendPswChangedAction:(NSString *)oP andNewPsw:(NSString *)newPsw completion:(void(^)(bool isSucc,NSString *info))completion{
    if(![AVUser currentUser])
    {
        completion(NO,@"请先登录");
        return;
    }
    
    [[AVUser currentUser] updatePassword:oP newPassword:newPsw block:^(id object, NSError *error) {
        if(!error)
        {
            completion(YES,@"修改成功!");
        }
        else{
            completion(NO,[error localizedDescription]);
        }
    }];
}

+ (void)valueForAVOSStaticType:(AVOSDefautsType)type block:(void(^)(NSString *resp))block{
    
    if(![[AVOSManger sharedManger] staticList])
    {
    AVQuery *query = [AVQuery queryWithClassName:@"Static"];
    
    NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] init];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if(!error)
        {
            for(AVObject *stcObj in objects)
            {
                [tmpDic setObject:stcObj[@"defaults"] forKey:stcObj[@"defaultsType"]];
            }
            
            [[self sharedManger ] setStaticList:[NSDictionary dictionaryWithDictionary:tmpDic]];
            
            block([[[self sharedManger] staticList] valueForDefaultsKey:type]);
        }
    }];
    }
    else
        block([[[self sharedManger] staticList] valueForDefaultsKey:type]);
}

- (void)setStaticList:(NSDictionary *)staticList_{
    staticList = staticList_;
}

- (void )queryVideoIdByObjectId:(NSString *)objId completion:(void(^)(NSString *))completion{
    AVQuery *vidQuery = [AVQuery queryWithClassName:@"Video"];
    
    [vidQuery whereKey:@"objectId" equalTo:objId];
    
    [vidQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        AVObject *obj = [objects lastObject];
        
        completion([obj objectId]);
    }];
}

- (void )queryVideoIdBySHA:(NSString *)sha completion:(void(^)(NSString *))completion{
    AVQuery *vidQuery = [AVQuery queryWithClassName:@"Video"];
    
    [vidQuery whereKey:@"videoId" equalTo:sha];
    
    [vidQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        AVObject *obj = [objects lastObject];
        
        completion([obj objectId]);
    }];
}

- (void)sendComment:(NSString *)cmt videoId:(NSString *)videoId userId:(NSString *)userId userSession:(NSString *)u_session completion:(void (^)(BOOL,NSString *))block{
    [AVOSManger valueForAVOSStaticType:AVOSDefautsTypeSendCmtURI block:^(NSString *resp){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:resp]];
            
            [request setRequestMethod:@"POST"];
            
            [request setTimeOutSeconds:3];
            
            [request addPostValue:videoId forKey:@"video_id"];
            
            [request addPostValue:u_session forKey:@"session"];
            
            [request addPostValue:cmt forKey:@"comment"];
            
            [request addPostValue:userId forKey:@"user_id"];
            
            [request startSynchronous];
            
            NSString *sig = [[NSString alloc]initWithData:[request responseData] encoding:NSUTF8StringEncoding];
            
            block(sig?YES:NO,sig);
        });
    }];
}

- (void)getCommentWithVideoId:(NSString *)videoId type:(AVOSVideoType)videoCmtType completion:(void (^)(NSUInteger respCode,NSDictionary *dic))block{
    
    NSString *appendStr = @"";
    
    if(videoCmtType == AVOSVideoTypeBest)
    {
        appendStr = [appendStr stringByAppendingString:@"?lim=2&order=-supportCount"];
    }
    
    [AVOSManger valueForAVOSStaticType:AVOSDefautsTypeGetCmtURI block:^(NSString *resp){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            NSURL *paramentURL = [NSURL URLWithString:[resp stringByAppendingString:appendStr]];
            
            ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:paramentURL];
            
            [request setRequestMethod:@"POST"];
            
            [request addPostValue:videoId forKey:@"v_id"];
            
            //[TLAlertHelper ];
            
            [request startSynchronous];
            
            NSDictionary *resp_dic = [NSDictionary convertJSONData:[request responseData]];
            
            block(request.responseStatusCode,resp_dic);
        });
    }];
}

- (void)getCommentWithVideoId:(NSString *)videoId completion:(void (^)(NSUInteger respCode,NSDictionary *dic))block{
    
    [self getCommentWithVideoId:videoId type:AVOSVideoTypeBest completion:^(NSUInteger respCode, NSDictionary *dic) {
        block(respCode,dic);
    }];
    /*
    [AVOSManger valueForAVOSStaticType:AVOSDefautsTypeGetCmtURI block:^(NSString *resp){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:resp]];
            
            [request setRequestMethod:@"POST"];
            
            [request addPostValue:videoId forKey:@"v_id"];
            
//            [TLAlertHelper ];
            
            [request startSynchronous];
            
            NSDictionary *resp_dic = [NSDictionary convertJSONData:[request responseData]];
            
            block(request.responseStatusCode,resp_dic);
        });
    }];
     */
}

#pragma mark -- Video Support

- (void)getSupportAtVideoId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum))block isLiked:(void(^)(BOOL isLiked,BOOL canLike))l_block{
    if([AVUser currentUser])
    {
        AVRelation *likedRelations = [[AVUser currentUser] relationforKey:@"likeVideos"];
        
        AVQuery *query =[likedRelations query];
        [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
            if(object &&!error)
                l_block(YES,YES);
            else
                l_block(NO,YES);
        }];
    }
    else
        l_block(NO,NO);
    
    AVQuery *query = [AVQuery queryWithClassName:@"Video"];
    
    [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
         block([object[@"likes"] unsignedIntegerValue]);
    }];
}

- (void)supportVideoId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum,BOOL succ))block{
    if(!v_id)
    {
        [TLAlertHelper showErrorHUDWithText:@"内部错误,赞失败"];
        return;
    }
    
    AVQuery *query = [AVQuery queryWithClassName:@"Video"];
    //the Video Object which User Like
    [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
        __block AVUser *currentUser = [AVUser currentUser];
        
        /*
        if(!currentUser)
        {
            UITabBarController *tabbarController= (UITabBarController *)[[[UIApplication sharedApplication]  keyWindow] rootViewController];
            [TLLoginViewController showLoginView:tabbarController.selectedViewController completion:^(BOOL isLogin) {
                if(isLogin)
                {
                    currentUser = [AVUser currentUser];
                    
                    AVRelation *likeVideos = [currentUser relationforKey:@"likeVideos"];
                    [likeVideos addObject:object];
                    [currentUser saveInBackground];
                    
                    NSNumber *sptNum = object[@"likes"];
                    
                    [object setObject:[sptNum increment] forKey:@"likes"];
                    
                    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        
                        block([sptNum unsignedIntegerValue]+1,succeeded);
                    }];
                    
                    [tabbarController.selectedViewController dismissViewControllerAnimated:YES completion:^{
                        [TLAlertHelper showSuccessHUDWithText:@"点赞成功!"];
                    }];
                }
            }];
        }*/
        
        
        if([currentUser isAuthenticated])
        {
            AVRelation *likeVideos = [currentUser relationforKey:@"likeVideos"];
            [likeVideos addObject:object];
            [currentUser saveInBackground];
            
            NSNumber *sptNum = object[@"likes"];
            
            [object setObject:[sptNum increment] forKey:@"likes"];
            
            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                
                block([sptNum unsignedIntegerValue]+1,succeeded);
                
                dispatch_async(dispatch_get_main_queue(), ^{
                   if(succeeded)
                       [TLAlertHelper showSuccessHUDWithText:@"点赞成功!"];
                    else
                        [TLAlertHelper showErrorHUDWithText:[error localizedDescription]];
                });
            }];
        }
    }];
}

- (void)cancleVideoId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum,BOOL succ))block{
    
    AVUser *currentUser = [AVUser currentUser];
    
    if(currentUser)
    {
    AVQuery *query = [AVQuery queryWithClassName:@"Video"];
    
    [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
        NSNumber *sptNum = object[@"likes"];
        
        [object setObject:[sptNum decrement] forKey:@"likes"];
        
        [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
           // block([sptNum unsignedIntegerValue]-1,succeeded);
            if([currentUser isAuthenticated])
            {
                AVRelation *likeVideos = [currentUser relationforKey:@"likeVideos"];
                [likeVideos  removeObject:object];
                [currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                     block([sptNum unsignedIntegerValue]-1,succeeded);
             }];
            }
         }];
        
      }];
   }
}


#pragma mark -- Comment Support

- (void)getSupportAtCmtId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum))block isLiked:(void(^)(BOOL isLiked,BOOL canLike))l_block{
    if([AVUser currentUser])
    {
        AVRelation *likedRelations = [[AVUser currentUser] relationforKey:@"likeCmts"];
        
        AVQuery *query =[likedRelations query];
        [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
            if(object &&!error)
                l_block(YES,YES);
            else
                l_block(NO,YES);
        }];
    }
    else
        l_block(NO,NO);
    
    AVQuery *query = [AVQuery queryWithClassName:@"Comment"];
    
    [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
        block([object[@"likes"] unsignedIntegerValue]);
    }];
}

- (void)supportCmtId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum,BOOL succ))block{
    if(!v_id)
    {
        [TLAlertHelper showErrorHUDWithText:@"内部错误,赞失败"];
        return;
    }
    
    AVQuery *query = [AVQuery queryWithClassName:@"Comment"];
    //the Video Object which User Like
    [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
        __block AVUser *currentUser = [AVUser currentUser];
        
        /*
         if(!currentUser)
         {
         UITabBarController *tabbarController= (UITabBarController *)[[[UIApplication sharedApplication]  keyWindow] rootViewController];
         [TLLoginViewController showLoginView:tabbarController.selectedViewController completion:^(BOOL isLogin) {
         if(isLogin)
         {
         currentUser = [AVUser currentUser];
         
         AVRelation *likeVideos = [currentUser relationforKey:@"likeVideos"];
         [likeVideos addObject:object];
         [currentUser saveInBackground];
         
         NSNumber *sptNum = object[@"likes"];
         
         [object setObject:[sptNum increment] forKey:@"likes"];
         
         [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
         
         block([sptNum unsignedIntegerValue]+1,succeeded);
         }];
         
         [tabbarController.selectedViewController dismissViewControllerAnimated:YES completion:^{
         [TLAlertHelper showSuccessHUDWithText:@"点赞成功!"];
         }];
         }
         }];
         }*/
        
        
        if([currentUser isAuthenticated])
        {
            AVRelation *likeVideos = [currentUser relationforKey:@"likesCmts"];
            [likeVideos addObject:object];
            [currentUser saveInBackground];
            
            NSNumber *sptNum = object[@"supportCount"];
            
            [object setObject:[sptNum increment] forKey:@"supportCount"];
            
            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                block([sptNum unsignedIntegerValue]+1,succeeded);
                
            dispatch_async(dispatch_get_main_queue(), ^{
                    if(succeeded)
                        [TLAlertHelper showSuccessHUDWithText:@"点赞成功!"];
                    else
                        [TLAlertHelper showErrorHUDWithText:[error localizedDescription]];
                });
            }];
        }
        
        
    }];
}

- (void)cancleCmtId:(NSString *)v_id completion:(void(^)(NSUInteger sptNum,BOOL succ))block{
    
    AVUser *currentUser = [AVUser currentUser];
    
    if(currentUser)
    {
        AVQuery *query = [AVQuery queryWithClassName:@"Comment"];
        
        [query getObjectInBackgroundWithId:v_id block:^(AVObject *object, NSError *error) {
            NSNumber *sptNum = object[@"supportCount"];
            
            [object setObject:[sptNum decrement] forKey:@"supportCount"];
            
            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                block([sptNum unsignedIntegerValue]-1,succeeded);
            }];
        }];
    }
}

#pragma mark -- Comment

- (void)updateLocalVideoList:(NSUInteger)lim localPlistPath:(NSString *)path completion:(VideoAVOSListUpdateSucc)completion{
    
   NSMutableArray* t_v_urls = [[NSMutableArray alloc] init];
   NSMutableArray* t_v_names = [[NSMutableArray alloc] init];
   NSMutableArray* t_pub_user = [[NSMutableArray alloc] init];
   NSMutableArray* t_v_ctimes = [[NSMutableArray alloc] init];
   NSMutableArray* t_v_descs = [[NSMutableArray alloc] init];
   NSMutableArray* t_likes = [[NSMutableArray alloc] init];
   NSMutableArray* t_indexs = [[NSMutableArray alloc] init];
   NSMutableArray* t_v_ids = [[NSMutableArray alloc] init];
   NSMutableArray* t_v_cate = [[NSMutableArray alloc] init];
    NSMutableArray* t_cmt_count = [[NSMutableArray alloc] init];
    
    __block NSUInteger videoCount = 0;
    
    AVQuery *query = [AVQuery queryWithClassName:@"Video"];
    
    //提取objectid 从plist文件
    NSArray *videoIdArr = [[NSArray alloc] initWithContentsOfFile:path];
    
    [query whereKey:@"objectId" containedIn:videoIdArr];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for(AVObject *obj in objects)
        {
            [t_v_descs addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeContent]]];
            [t_v_names addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoName]]];
            [t_v_urls addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoURL]]];
            [t_pub_user addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypePubUser]]];
            [t_v_ctimes addObject:[obj dateValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeCreatedAt]]];
            [t_likes addObject:[obj numberValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeLikes]]];
            [t_v_ids addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoId]]];
            [t_indexs addObject:[obj numberValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeIndex]]];
            [t_v_cate addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeCategory]]];
            //get comment count
            AVRelation *cmt_relation = [obj objectForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeComments]];
            AVQuery *reverseQuery = [cmt_relation query];
            
            NSUInteger count = [reverseQuery countObjects];
            
            [t_cmt_count addObject:[NSNumber numberWithUnsignedInteger:count]];
        }
        videoCount = [v_urls count];
        
        completion(t_v_urls,t_v_names,t_pub_user,t_v_ctimes,t_v_descs,t_likes,t_indexs,t_v_ids,t_v_cate,t_cmt_count,videoCount);
    }];
}


#pragma mark -- Video Information Get
- (void)updateVideoListsFromAVOSLimition:(NSUInteger)limition type:(AVOSVideoType)v_type updateCompletion:(VideoAVOSListUpdateSucc)completion{
    
    __block NSUInteger videoCount = 0;
    
    [self removeVideoList];
    
    AVQuery *query = [AVQuery queryWithClassName:@"Video"];
    
    query.limit = limition;
    
    if(v_type != AVOSVideoTypeAll)
    [query whereKey:@"Category" equalTo:[self enumCategoryToString:v_type]];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for(AVObject *obj in objects)
        {
            [v_descs addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeContent]]];
            [v_names addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoName]]];
            [v_urls addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoURL]]];
            [pub_user addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypePubUser]]];
            [v_ctimes addObject:[obj dateValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeCreatedAt]]];
            [likes addObject:[obj numberValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeLikes]]];
            [v_ids addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeVideoId]]];
            [v_cate addObject:[obj stringValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeCategory]]];
            AVRelation *cmt_relation = [obj objectForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeComments]];
            AVQuery *reverseQuery = [cmt_relation query];
            
            NSUInteger count = [reverseQuery countObjects];
            
            [v_cmt_count addObject:[NSNumber numberWithUnsignedInteger:count]];

            [indexs addObject:[obj numberValueForKey:[obj stringWithAVOSVideoInfoType:AVOSVideoInfoTypeIndex]]];
        }
        
        videoCount = [v_urls count];
        
        completion(v_urls,v_names,pub_user,v_ctimes,v_descs,likes,indexs,v_ids,v_cate,v_cmt_count,videoCount);
    }];
}

#pragma 

- (void)removeVideoList{

    [v_urls removeAllObjects];
    [v_names removeAllObjects];
    [pub_user removeAllObjects];
    [v_ctimes removeAllObjects];
    [v_descs removeAllObjects];
    [likes removeAllObjects];
    [indexs removeAllObjects];
    [v_ids removeAllObjects];
    [v_cate removeAllObjects];
    [v_cmt_count removeAllObjects];
}


/**
 *  更新视频获取的URL
 *
 *  @param videoListCount   Video URL count
 *  @param updateCompletion Video List Update Completion
 */
- (void)updateVideoList:(NSUInteger)videoListCount updateCompletion:(VideoListUpdateSucc)updateCompletion{
    TXYRequestManger *tmanger = [TXYManger sharedRequestManger];
    
    NSDictionary *postBodys = @{@"bucket":@"testzone",@"limit":@"10",@"v_type":@"-1"};
    
    [tmanger requestJSONDataFromURL:@"http://1.btcnotification.sinaapp.com/getVideoList.php" secretId:TXYSID  postBodys:postBodys completion:^(TXYResponse *resp) {
        NSLog(@"The HTTPCode is %ld the data is %@ the message is %@",(unsigned long)resp.httpcode,resp.data,resp.message);
        
        if(resp.code == TXYRespCodeSucc)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                //update video table
                NSArray<NSString *> *v_urls_ = [[[resp getRespData] getVideoInfos] extractVideoURLFromTXYResponseInfos];
                NSArray *v_descs_= [[[resp getRespData] getVideoInfos] extractVideoDescriptionFromTXYResponseInfos];
                NSArray *v_ctimes_ = [[[resp getRespData] getVideoInfos] extractVideoUpdateTimeFromTXYResponseInfos];
                NSArray *v_names_ = [[[resp getRespData] getVideoInfos] extractVideoNameFromTXYResponseInfos];
                NSArray *v_ids_ = [[[resp getRespData] getVideoInfos] extractVideoSHAFromTXYResponseInfos];
                
        updateCompletion(v_urls_,v_names_,v_ctimes_,v_descs_,v_ids_,v_urls_.count);
            });
        }
        else
        {
            NSLog(@"数据请求错误");
            /**
             *  显示alertview
             */
        }
    }];
}

- (void)resetPasswordByVerifyCode:(NSString *)vfCode newPsw:(NSString *)newPsw completion:(void(^)(BOOL isSucc,NSError *error))completion{
    [AVUser resetPasswordWithSmsCode:vfCode newPassword:newPsw block:^(BOOL succeeded, NSError *error) {
        if(succeeded)
        completion(YES,nil);
        else
        completion(NO,error);
    }];
}

#pragma mark -
#pragma mark -- 提交视频信息
- (void)submitVideoURL:(NSString *)v_url{
    AVObject *submit = [AVObject objectWithClassName:@"Feed"];
    
    [submit setObject:v_url forKey:@"FeedContent"];
    [submit setObject:[AVUser currentUser] forKey:@"Contributer"];
    
    [submit saveInBackground];
}

#pragma mark -
#pragma mark -- 获取当前视频分类
- (AVOSVideoType)videoType{
    NSString *str =  [[NSUserDefaults standardUserDefaults] objectForKey:VideoTypeKey];
    
    return str ? [[AVOSManger sharedManger] categoryStringToEnum:str]: AVOSVideoTypeAll;
}
- (BOOL)syncVideoType:(NSString *)str_type{
    [[NSUserDefaults standardUserDefaults] setObject:str_type forKey:VideoTypeKey];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
